# -*- coding: utf-8 -*-
# בש״ד
#
# Miraflores Blog for Django - Part of the Cuba Libre Project
# Copyright (C) 2009, Joshua "jag" Ginsberg <jag@flowtheory.net>
# 
# Por mi amor, que inspira a mi cada respiración y que echa de Cuba
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from django.contrib import admin
from django import forms
from miraflores.models import BlogEntry, BlogEntryUpdate, Category
from miraflores import settings
from tagging.utils import edit_string_for_tags
from markitup.forms import MarkItUpWidget

class BlogEntryUpdateForm(forms.ModelForm):
    body = forms.CharField(widget=MarkItUpWidget)

class BlogEntryUpdateAdmin(admin.StackedInline):
    form = BlogEntryUpdateForm
    model = BlogEntryUpdate
    extra = 1

class BlogEntryAdminForm(forms.ModelForm):
    body = forms.CharField(widget=MarkItUpWidget)
    tags = forms.CharField(label=u'Tags', max_length=200, required=False,
                           help_text=(u'Begin typing a tag, and a list of '
                                      u'existing similar tags will appear. '
                                      u'Click on any in the list to add it. '
                                      u'<br />'
                                      u'Sample syntax:<br />'
                                      u'tag1 tag2 tag3<br />'
                                      u'single "double word"<br />'
                                      u'single, double word'))
    
    def __init__(self, *args, **kwargs):
        instance = kwargs.get('instance', None)
        initial = kwargs.get('initial', {})
        if instance:
            initial['tags'] = edit_string_for_tags(instance.tags)
            kwargs.update({'initial': initial, 'instance': instance})
        super(BlogEntryAdminForm, self).__init__(*args, **kwargs)
    
    def save(self, *args, **kwargs):
        tags = self.cleaned_data.pop('tags', u'')
        obj = super(BlogEntryAdminForm, self).save(*args, **kwargs)
        obj.save()
        obj.tags = tags
        return obj

    class Meta:
        model = BlogEntry

class BlogEntryAdmin(admin.ModelAdmin):
    inlines = [BlogEntryUpdateAdmin]
    form = BlogEntryAdminForm
    
    class Media:
        css = {
            "all": (settings.TAGS_CSS_URL,),
        }
        js = (settings.JQUERY_URL,
              settings.JQUERY_TAGS_URL)

admin.site.register(BlogEntry, BlogEntryAdmin)
admin.site.register(Category)